package com.zjlanyun.erp;

import android.app.Activity;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.SharedPreferences;

import com.yolanda.nohttp.Logger;
import com.yolanda.nohttp.NoHttp;
import com.zjlanyun.erp.rfid.m10.u8.reader.server.ReaderHelper;
import com.zjlanyun.erp.rfid.m10.u8.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanyun on 16/4/27.
 */
public class MainApplication extends Application {

    private static Context mContext;
    private static Application _instance;
    private List<Activity> activities = new ArrayList<Activity>();

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        _instance = this;
        Common.checkDeviceToken(getApplicationContext());
        NoHttp.initialize(this);
        Logger.setTag("NoHttp");
        Logger.setDebug(true);

        //RFID
        Tools.InitMedia(mContext);
        try {
            ReaderHelper.setContext(mContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Context getContext(){
        return mContext;
    }

    public static Application getInstance() {
        return _instance;
    }

    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        for (Activity activity : activities) {
            try {
                activity.finish();
            } catch (Exception e) {
                ;
            }
        }

        if (BluetoothAdapter.getDefaultAdapter() != null)
            BluetoothAdapter.getDefaultAdapter().disable();

        Tools.stopMedia();
        System.exit(0);
    }

    static public void saveBeeperState(int state){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_state", state);
        editor.commit();
    }

    static int _SoftSound=2;
    static public int appGetSoftSound(){
        if(_SoftSound==2)
            _SoftSound=getSoftSound();
        return _SoftSound;
    }

    static public int getVeeperState(){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        int state = spf.getInt("_state", 0);
        return state;
    }

    static public void saveSoftSound(int state){
        _SoftSound=state;
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_software_sound", state);
        editor.commit();
    }

    static public int getSoftSound(){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        int state = spf.getInt("_software_sound", 1);
        return state;
    }

    static public void saveSessionState(int state){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_session", state);
        editor.commit();
    }

    static public int getSessionState(){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        //SharedPreferences.Editor editor = spf.edit();
        int state = spf.getInt("_session", 0);
        return state;
    }
    static public void saveFlagState(int state){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_flag", state);
        editor.commit();
    }

    static public int getFlagState(){
        SharedPreferences spf = mContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        //SharedPreferences.Editor editor = spf.edit();
        int state = spf.getInt("_flag", 0);
        return state;
    }
}
